<template>
  <div>
    <div class="box-bar">
      <div class="box-left" @click="changeList('left')">
        <i class="el-icon-d-arrow-left" style="cursor: pointer;"></i>
      </div>
      <div class="box-view">
        <div class="box-content">
          <div class="box" v-for="(item, idx) in barList" :key="idx" :class="idx == barChoose ? 'actived' : ''"
            @click="changeChart(idx)">
            {{ item }}
          </div>
        </div>
      </div>
      <div class="box-right" @click="changeList('right')">
        <i class="el-icon-d-arrow-right" style="cursor: pointer;"></i>
      </div>
    </div>
    <Echart id="leftCenter" v-if="pageflag" :options="options" class="left_center_inner" ref="charts" height="240px" />
    <Reacquire v-else @onclick="getData" style="line-height: 200px">
      重新获取
    </Reacquire>
  </div>

</template>

<script>
import { colorsA, colorsB, legend, xAxis, yAxis } from '@/config/EChartVar';

export default {
  data() {
    return {
      options: {},
      barList: ['钢材类', '五金类', '灯具类', '板材类', '电器类', '家具类', '其他类', '用水量'],
      chartType: 0,
      barChoose: 0,
      countUserNumData: {
        axisData: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月'].reverse(),
        seriesData: [58, 61, 89, 70, 65, 35, 60, 85],
        seriesData0: [68, 59, 51, 39, 75, 68, 80, 60],
        seriesData1: [40, 50, 30, 20, 75, 75, 80, 60],
        seriesData2: [80, 20, 40, 30, 75, 80, 77, 60],
      },
      pageflag: true,
      timer: null,
    };
  },
  created() {
    this.getData();
  },
  mounted() { },
  beforeDestroy() {
    this.clearData();
  },
  methods: {
    changeList(type) {
      if (type === 'left') {
        this.barList.unshift(this.barList.pop());
      } else {
        this.barList.push(this.barList.shift());
      }
    },
    changeChart(chartType) {
      this.barChoose = chartType;
      this.options.legend.data[0] = this.barList[chartType];
      this.options.series[0].name = this.barList[chartType];
      this.init(this.countUserNumData, chartType)
    },
    clearData() {
      if (this.timer) {
        clearInterval(this.timer);
        this.timer = null;
      }
    },
    getData() {
      this.pageflag = true;
      this.$nextTick(() => {
        this.init(this.countUserNumData, this.barChoose);
      });
    },
    //轮询
    switper() {
      if (this.timer) {
        return;
      }
      let looper = (a) => {
        this.getData();
      };
      this.timer = setInterval(
        looper,
        2000 // this.$store.state.setting.echartsAutoTime // staticAutoTime
      );
      let myChart = this.$refs.charts.chart;
      myChart.on('mouseover', (params) => {
        this.clearData();
      });
      myChart.on('mouseout', (params) => {
        this.timer = setInterval(
          looper,
          2000 // this.$store.state.setting.echartsAutoTime // staticAutoTime
        );
      });
    },
    init(currentData, barChoose) {
      let axisData = currentData.axisData;
      let seriesData = [
        [...currentData.seriesData],
        [...currentData.seriesData0],
        [...currentData.seriesData1],
        [...currentData.seriesData2],
      ];

      const options = {
        grid: {
          top: '8%',
          left: '2%',
          right: '4%',
          bottom: '10%',
          containLabel: true,
        },
        legend: {
          show: false,
          top: '1%',
          left: '4%',
          icon: 'circle',
          itemWidth: 12,
          itemHeight: 12,
          // itemGap: 60,
          textStyle: legend.textStyle,
          data: ['运输成本'],
        },
        xAxis: {
          type: 'category',
          data: axisData,
          boundaryGap: false,
          splitLine: {
            show: false,
          },
          axisTick: {
            show: false, // 不显示下切线
          },
          axisLabel: xAxis.axisLabel,
          axisLine: {
            lineStyle: {
              background: 'rgba(176, 215, 255, 0.4)',
            },
          },
        },
        yAxis: [
          {
            type: 'value',
            max: 100,
            min: 0, //
            interval: 20,
            axisLabel: yAxis.axisLabel,
            axisLine: {
              show: false,
              // interval: 1,
              lineStyle: {
                color: 'rgba(0,0,0,0.5)',
              },
            },
            splitLine: {
              show: true,
              // interval: 1,
              lineStyle: {
                color: ['rgba(176, 215, 255, 0.2)'],
                width: 1,
                type: 'dashed',
              },
            },
            axisTick: {
              show: false,
            },
          },
        ],
        series: [],
      };
      const series = [
        {
          name: '',
          type: 'line',
          // 动画配置
          animation: true,
          animationDuration: 2000,
          animationEasing: 'quarticInOut',
          animationDelay: function (idx) {
            return idx * 150;
          },
          smooth: false,
          showSymbol: false,
          // symbol: 'circle',
          // symbolSize: 6,
          data: seriesData[barChoose],
          itemStyle: {
            normal: {
              // color: "#38FFBA",
              color: 'rgba(0,219,254, .7)',
            },
          },
          areaStyle: {
            color: colorsB[0],
          },
          lineStyle: {
            normal: {
              width: 2,
            },
          },
          label: {
            show: false,
            position: 'top',
            formatter: (par) => {
              return par.value;
            },
            textStyle: { fontSize: 12, color: 'rgba(0,219,254, 1)' },
          },
          markPoint: {
            symbol: 'circle',
            symbolSize: [14, 14], // 容器大小
            symbolOffset: ['0%', '0%'],
            data: [
              {
                name: '最大值',
                type: 'max',
                valueIndex: 1,
              },
            ],
            label: {
              show: true,
              position: 'top',
              fontSize: 12,
              color: 'rgba(0,219,254, 1)',
              formatter: (par) => {
                return par.value;
              },
            },
          },
        },
      ];
      options.series = series;
      this.options = options;
    },
  },
};
</script>
<style lang="scss" scoped>
.box-bar {
  width: 100%;
  display: flex;
  z-index: 999;
  // justify-content: center;
  align-items: center;
  padding-left: 20px;

  .box-view {
    width: 480px;
    overflow: hidden;

    .box-content {
      width: 2800px;
      display: flex;
    }
  }


  .box {
    width: 56px;
    height: 20px;
    margin: 0 2px;
    text-align: center;
    line-height: 20px;
    font-size: 10px;
    cursor: pointer;
    color: #fff;
    // display: inline-block;
    position: relative;
    width: 56px;
    height: 20px;
    z-index: 3;
    background: rgba(255, 255, 255, 0.05);

    &.actived {
      background-color: #154FC1;
    }
  }
}
</style>
